package com.ylsk.system.helper;

import com.google.common.collect.Maps;
import com.ylsk.common.config.MinioConfig;
import com.ylsk.common.config.SnowIdConfig;
import com.ylsk.common.exception.ServiceException;
import com.ylsk.common.utils.MinioUtil;
import com.ylsk.common.utils.StringUtils;
import com.ylsk.common.utils.spring.SpringUtils;
import com.ylsk.system.domain.BusinessFile;
import com.ylsk.system.domain.FileInfo;
import com.ylsk.system.domain.FileSimple;
import com.ylsk.system.mapper.BusinessFileMapper;
import com.ylsk.system.mapper.FileInfoMapper;
import com.ylsk.system.service.IMinioFileService;
import org.apache.commons.compress.utils.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class FileHelper {

    private static final Logger log = LoggerFactory.getLogger(FileHelper.class);

    @Autowired
    private BusinessFileMapper businessFileMapper;

    @Autowired
    private SnowIdConfig snowIdConfig;

    @Autowired
    private IMinioFileService minioFileService;

    private static MinioConfig minio = SpringUtils.getBean(MinioConfig.class);

    @Autowired
    private MinioUtil minioUtil;

    @Autowired
    private FileInfoMapper fileInfoMapper;
    /**
     * 新增业务附件数据
     * @param fileIds 附件id集合
     * @param businessId 业务数据id
     * @return
     */
    public int saveBusinessFileData(String fileIds,Long businessId){
        if(StringUtils.isNotEmpty(fileIds) && !fileIds.equals("[]")){
            //附件id集合
            List<String> fileIdList = Arrays.asList(StringUtils.split(fileIds, ","));
            List<BusinessFile> businessFileList = Lists.newArrayList();
            for (String s : fileIdList) {
                BusinessFile businessFile = new BusinessFile();
                businessFile.setBusinessFileId(snowIdConfig.getSnowId());
                businessFile.setFileId(Long.valueOf(s));
                businessFile.setModelId(businessId);
                businessFile.setModelName("测试");
                businessFile.setCreateTime(new Date());
                businessFile.setUpdateTime(new Date());
                businessFileList.add(businessFile);
            }
            return businessFileMapper.batchInsertBusinessFile(businessFileList);
        }
        return 0;
    }



    /**
     * 修改业务附件数据
     * @param  deleteFileIds 要删除附件id集合
     * @param fileIds 附件id集合
     * @param businessId 业务数据id
     * @return
     */
    public int updateBusinessFileData(String deleteFileIds,String fileIds,Long businessId){
        log.debug("更新业务数据附件");
        if(null == businessId){
            throw new ServiceException("businessId 不能为空");
        }
//        if(StringUtils.isNotEmpty(deleteFileIds)){
//            //先删除相关的业务数据附件
//            Map<String,Object> map = Maps.newHashMap();
//            map.put("model_id",businessId);
//            businessFileMapper.deleteByMap(map);
//        }


        //先删除相关的业务数据附件
        Map<String,Object> map = Maps.newHashMap();
        map.put("model_id",businessId);
        businessFileMapper.deleteByMap(map);
        return saveBusinessFileData(fileIds,businessId);
    }


    /**
     * 修改业务附件数据
     * @param fileIds 附件id集合
     * @param businessId 业务数据id
     * @return
     */
    public int updateBusinessFileData(String fileIds,Long businessId){
        log.debug("更新业务数据附件");
        if(null == businessId){
            throw new ServiceException("businessId 不能为空");
        }
        //先删除相关的业务数据附件
        Map<String,Object> map = Maps.newHashMap();
        map.put("model_id",businessId);
        businessFileMapper.deleteByMap(map);
        if(StringUtils.isNotEmpty(fileIds)){
            return saveBusinessFileData(fileIds,businessId);
        }
        return 0 ;
    }

    /**
     * 头像预览地址
     * @param filePath
     * @return
     */
    public String packageAvatar(String filePath){
        return minioUtil.getFileUrl(minio.getBucketName(), filePath);
    }


    /**
     * 获取业务数据 附件信息
     * @param businessId
     * @return
     */
    public List<FileSimple> getFileByBusinessId(Long businessId){
        if(null == businessId){
            return Lists.newArrayList();
        }
        return minioFileService.getFileListByBusinessId(businessId);
    }

    public Map<Long,List<FileSimple>> getFileInBusinessId(List<Long> businessId){

        minioFileService.getFileListInBusinessId(businessId);
        return Maps.newConcurrentMap();
    }


    public FileInfo getFileInfo(Long fileId){
        return fileInfoMapper.selectById(fileId);
    }
}
